package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoGbt;
import com.anhuanjia.tools.eao.bean.EaoManagement;
import com.anhuanjia.tools.eao.bean.EaoReportType;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

@Alias(value = "reportType")
public class ReportTypeUI extends UIFacade {

	/**
	 * 新增/更新报告类型
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdateReportType() {
		Transaction transaction = new Transaction();
		EaoReportType reportType = new EaoReportType();
		reportType.setValue(this.Request);
		String Report_id = reportType.getReportId();
		if (StringUtils.isEmpty(Report_id)) {
			reportType.setReportId(UUID.randomUUID().toString());
			transaction.insert(reportType);
			transaction.commit();
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q(" select * from eao_report_type where ReportId = ?", Report_id).fetch();
			if (dt.getRowCount() != 0) {
				EaoReportType entity = new EaoReportType();
				entity.setValue(dt.get(0));
				entity.setValue(this.Request);
				transaction.update(entity);
				transaction.commit();
				if (transaction.commit()) {
					s("更新成功");
				} else {
					f("更新失败");
				}
			}
		}
	}

	/**
	 * 查询所有报告类型菜单列表
	 */
	@Priv(loginType = LoginType.Member)
	public void findAllMenu() {
		DataTable dt = new Q(" select * from eao_report_type where Tid = ' ' or Tid is null ").fetch();
		List<Map<String, Object>> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoReportType reportType = new EaoReportType();
			reportType.setValue(dataRow);
			Map<String, Object> reportTypeVo = new HashMap<>();
			reportTypeVo.put("value", reportType.getReportId());
			reportTypeVo.put("label", reportType.getItem());
			list.add(reportTypeVo);
			List<Map<String, Object>> reportTypeVo2List = new ArrayList<>();
			DataTable dt2 = new Q(" select * from eao_report_type where Tid = ? ", reportType.getReportId()).fetch();
			if (dt2.getRowCount() > 0) {
				for (DataRow dataRow2 : dt2) {
					EaoReportType reportType2 = new EaoReportType();
					reportType2.setValue(dataRow2);
					Map<String, Object> reportTypeVo2 = new HashMap<>();
					reportTypeVo2.put("value", reportType2.getReportId());
					reportTypeVo2.put("label", reportType2.getItem());
					reportTypeVo2List.add(reportTypeVo2);
					DataTable dt3 = new Q(" select * from eao_report_type where Tid = ? ", reportType2.getReportId())
							.fetch();
					List<Map<String, Object>> reportTypeVo3List = new ArrayList<>();
					if (dt3.getRowCount() > 0) {
						for (DataRow dataRow3 : dt3) {
							EaoReportType reportType3 = new EaoReportType();
							reportType3.setValue(dataRow3);
							Map<String, Object> reportTypeVo3 = new HashMap<>();
							reportTypeVo3.put("value", reportType3.getReportId());
							reportTypeVo3.put("label", reportType3.getItem());
							reportTypeVo3List.add(reportTypeVo3);
							List<Map<String, Object>> reportTypeVo4List = new ArrayList<>();
							if (!reportTypeVo4List.isEmpty()) {
								reportTypeVo3.put("children", reportTypeVo4List);
							}
						}
					}
					if (!reportTypeVo3List.isEmpty()) {
						reportTypeVo2.put("children", reportTypeVo3List);
					}
				}
			}
			if (!reportTypeVo2List.isEmpty()) {
				reportTypeVo.put("children", reportTypeVo2List);
			}
			this.Response.put("data", list);
		}
	}

	/**
	 * 查询所有二级报告类型
	 */
	@Priv(loginType = LoginType.Member)
	public void findAllSon() {
		DataTable dt = new Q(" select * from eao_report_type where Tid = ' ' or Tid is null ").fetch();
		List<EaoReportType> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoReportType reportType = new EaoReportType();
			reportType.setValue(dataRow);
			DataTable dataTable = new Q(" select * from eao_report_type where Tid = ? ", reportType.getReportId())
					.fetch();
			for (DataRow dr : dataTable) {
				EaoReportType entity = new EaoReportType();
				entity.setValue(dr);
				System.out.println(entity);
				list.add(entity);
			}
		}
		this.Response.put("data", list);
	}

	/**
	 * 根据id查询报告类型
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String Report_id = $V("ReportId");
		List<EaoReportType> list = new ArrayList<>();
		DataTable dt = new Q(" select * from eao_report_type where ReportId = ?", Report_id).fetch();
		if (dt.getRowCount() != 0) {
			EaoReportType reportType = new EaoReportType();
			reportType.setValue(dt.get(0));
			this.Response.put("data", reportType);
			if (StringUtils.isEmpty(reportType.getTid())) {
				DataTable dataTable = new Q(" select * from eao_report_type where Tid = ?", Report_id).fetch();
				for (DataRow dataRow : dataTable) {
					EaoReportType entity = new EaoReportType();
					entity.setValue(dataRow);
					list.add(entity);
				}
				this.Response.put("son", list);
			} else {
				DataTable dataTable = new Q(" select * from eao_report_type where ReportId = ?", reportType.getTid())
						.fetch();
				EaoReportType entity = new EaoReportType();
				entity.setValue(dataTable.get(0));
				list.add(entity);
				this.Response.put("parent", list);
			}
		} else {
			f("查询的项目不存在");
		}
	}

	/**
	 * 报告类型删除
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String Report_id = $V("ReportId");
		DataTable dataTable = new Q(" select * from eao_report_type where ReportId = ?", Report_id).fetch();
		if (dataTable.getRowCount() != 0) {
			EaoReportType entity = new EaoReportType();
			entity.setValue(dataTable.get(0));
			Transaction transaction = new Transaction();
			transaction.delete(entity);
			if (transaction.commit()) {
				s("类型删除成功");
			} else {
				f("类型删除失败");
			}
		} else {
			f("删除的项目不存在");
		}

	}

	/**
	 * 查询所有国民经济行业列表
	 */
	@Priv(loginType = LoginType.Member)
	public void GBTMenu() {
		String value = $V("Value");
		List<Map<String, Object>> list = new ArrayList<>();
		List<Map<String, Object>> reportTypeVoList = new ArrayList<>();
		if (StringUtils.isEmpty(value)) {
			DataTable dt = new Q(" select * from eao_gbt where Father = ' ' or Father is null ORDER BY SType ").fetch();
			for (DataRow dataRow : dt) {
				EaoGbt gbt = new EaoGbt();
				gbt.setValue(dataRow);
				Map<String, Object> reportTypeVo = new HashMap<>();
				reportTypeVo.put("value", gbt.getSType());
				reportTypeVo.put("label", gbt.getNumber() + " " + gbt.getTypeName());
				reportTypeVo.put("children", reportTypeVoList);
				reportTypeVo.put("loading", false);
				list.add(reportTypeVo);
			}
		}
		if (!StringUtils.isEmpty(value)) {
			DataTable dt3 = new Q(" select * from eao_gbt where Father = ? ", value).fetch();
			if (dt3.getRowCount() > 0) {
				for (DataRow dataRow3 : dt3) {
					EaoGbt gbt3 = new EaoGbt();
					gbt3.setValue(dataRow3);
					Map<String, Object> reportTypeVo3 = new HashMap<>();
					reportTypeVo3.put("value", gbt3.getSType());
					reportTypeVo3.put("label", gbt3.getNumber() + " " + gbt3.getTypeName());
					DataTable dt4 = new Q(" select * from eao_gbt where Father = ? ", gbt3.getSType()).fetch();
					if (dt4.getRowCount() > 0) {
						reportTypeVo3.put("children", reportTypeVoList);
						reportTypeVo3.put("loading", false);
					}
					list.add(reportTypeVo3);
				}
			}
		}
		this.Response.put("data", list);
	}

	/**
	 * 查询所有国民经济行业列表
	 */
	@Priv(loginType = LoginType.Member)
	public void GBTMenu2() {
		String Economy_industry_type = $V("EconomyIndustryType");
		if (StringUtil.isNotEmpty(Economy_industry_type)) {
			List<Map<String, Object>> list = new ArrayList<>();
			List<Map<String, Object>> reportTypeVoList = new ArrayList<>();
			List<Map<String, Object>> reportTypeVo2List = new ArrayList<>();
			List<Map<String, Object>> reportTypeVo3List = new ArrayList<>();
			List<Map<String, Object>> reportTypeVo4List = new ArrayList<>();
			String[] st = Economy_industry_type.split("\"");
			if (st.length == 3) {
				DataTable dt = new Q(" select * from eao_gbt where Father = ' ' or Father is null ORDER BY SType ")
						.fetch();
				if (dt.getRowCount() != 0) {
					for (DataRow dataRow : dt) {
						EaoGbt gbt = new EaoGbt();
						gbt.setValue(dataRow);
						Map<String, Object> reportTypeVo = new HashMap<>();
						reportTypeVo.put("value", gbt.getSType());
						reportTypeVo.put("label", gbt.getNumber() + " " + gbt.getTypeName());
						reportTypeVo.put("children", reportTypeVoList);
						reportTypeVo.put("loading", false);
						list.add(reportTypeVo);
					}
				}
			} else if (st.length == 5) {
				DataTable dt = new Q(" select * from eao_gbt where Father = ' ' or Father is null ORDER BY SType ")
						.fetch();
				for (DataRow dataRow : dt) {
					EaoGbt gbt = new EaoGbt();
					gbt.setValue(dataRow);
					Map<String, Object> reportTypeVo = new HashMap<>();
					reportTypeVo.put("value", gbt.getSType());
					reportTypeVo.put("label", gbt.getNumber() + " " + gbt.getTypeName());
					if (gbt.getSType().equals(st[1])) {
						DataTable dt1 = new Q("select * from eao_gbt where Father = ?", st[1]).fetch();
						for (DataRow dataRow2 : dt1) {
							EaoGbt gbt2 = new EaoGbt();
							gbt2.setValue(dataRow2);
							Map<String, Object> reportTypeVo2 = new HashMap<>();
							reportTypeVo2.put("value", gbt2.getSType());
							reportTypeVo2.put("label", gbt2.getNumber() + " " + gbt2.getTypeName());
							reportTypeVo2.put("children", reportTypeVo2List);
							reportTypeVo2.put("loading", false);
							reportTypeVoList.add(reportTypeVo2);
						}
						reportTypeVo.put("children", reportTypeVoList);
						reportTypeVo.put("loading", false);
					} else {
						reportTypeVo.put("children", reportTypeVo4List);
						reportTypeVo.put("loading", false);
					}
					list.add(reportTypeVo);
				}
			} else if (st.length == 7) {
				DataTable dt = new Q(" select * from eao_gbt where Father = ' ' or Father is null ORDER BY SType ")
						.fetch();
				for (DataRow dataRow : dt) {
					EaoGbt gbt = new EaoGbt();
					gbt.setValue(dataRow);
					Map<String, Object> reportTypeVo = new HashMap<>();
					reportTypeVo.put("value", gbt.getSType());
					reportTypeVo.put("label", gbt.getNumber() + " " + gbt.getTypeName());
					if (gbt.getSType().equals(st[1])) {
						DataTable dt1 = new Q("select * from eao_gbt where Father = ?", st[1]).fetch();
						for (DataRow dataRow2 : dt1) {
							EaoGbt gbt2 = new EaoGbt();
							gbt2.setValue(dataRow2);
							Map<String, Object> reportTypeVo2 = new HashMap<>();
							reportTypeVo2.put("value", gbt2.getSType());
							reportTypeVo2.put("label", gbt2.getNumber() + " " + gbt2.getTypeName());
							if (gbt2.getSType().equals(st[3])) {
								DataTable dt2 = new Q("select * from eao_gbt where Father = ?", st[3]).fetch();
								for (DataRow dataRow3 : dt2) {
									EaoGbt gbt3 = new EaoGbt();
									gbt3.setValue(dataRow3);
									Map<String, Object> reportTypeVo3 = new HashMap<>();
									reportTypeVo3.put("value", gbt3.getSType());
									reportTypeVo3.put("label", gbt3.getNumber() + " " + gbt3.getTypeName());
									DataTable dt3 = new Q("select * from eao_gbt where Father = ?", gbt3.getSType())
											.fetch();
									if (dt3.getRowCount() > 0) {
										reportTypeVo3.put("children", reportTypeVo3List);
										reportTypeVo3.put("loading", false);
									}
									reportTypeVo2List.add(reportTypeVo3);
								}
								reportTypeVo2.put("children", reportTypeVo2List);
								reportTypeVo2.put("loading", false);
							} else {
								reportTypeVo2.put("children", reportTypeVo4List);
								reportTypeVo2.put("loading", false);
							}
							reportTypeVoList.add(reportTypeVo2);
						}
						reportTypeVo.put("children", reportTypeVoList);
						reportTypeVo.put("loading", false);
					} else {
						reportTypeVo.put("children", reportTypeVo4List);
						reportTypeVo.put("loading", false);
					}
					list.add(reportTypeVo);
				}

			} else if (st.length == 9) {
				DataTable dt = new Q(" select * from eao_gbt where Father = ' ' or Father is null ORDER BY SType ")
						.fetch();
				for (DataRow dataRow : dt) {
					EaoGbt gbt = new EaoGbt();
					gbt.setValue(dataRow);
					Map<String, Object> reportTypeVo = new HashMap<>();
					reportTypeVo.put("value", gbt.getSType());
					reportTypeVo.put("label", gbt.getNumber() + " " + gbt.getTypeName());
					if (gbt.getSType().equals(st[1])) {
						DataTable dt1 = new Q("select * from eao_gbt where Father = ?", st[1]).fetch();
						for (DataRow dataRow2 : dt1) {
							EaoGbt gbt2 = new EaoGbt();
							gbt2.setValue(dataRow2);
							Map<String, Object> reportTypeVo2 = new HashMap<>();
							reportTypeVo2.put("value", gbt2.getSType());
							reportTypeVo2.put("label", gbt2.getNumber() + " " + gbt2.getTypeName());
							if (gbt2.getSType().equals(st[3])) {
								DataTable dt2 = new Q("select * from eao_gbt where Father = ?", st[3]).fetch();
								for (DataRow dataRow3 : dt2) {
									EaoGbt gbt3 = new EaoGbt();
									gbt3.setValue(dataRow3);
									Map<String, Object> reportTypeVo3 = new HashMap<>();
									reportTypeVo3.put("value", gbt3.getSType());
									reportTypeVo3.put("label", gbt3.getNumber() + " " + gbt3.getTypeName());
									if (gbt3.getSType().equals(st[5])) {
										DataTable dt3 = new Q("select * from eao_gbt where Father = ?", st[5]).fetch();
										for (DataRow dataRow4 : dt3) {
											EaoGbt gbt4 = new EaoGbt();
											gbt4.setValue(dataRow4);
											Map<String, Object> reportTypeVo4 = new HashMap<>();
											reportTypeVo4.put("value", gbt4.getSType());
											reportTypeVo4.put("label", gbt4.getNumber() + " " + gbt4.getTypeName());
											reportTypeVo3List.add(reportTypeVo4);
										}
										reportTypeVo3.put("children", reportTypeVo3List);
										reportTypeVo3.put("loading", false);
									} else {
										DataTable dt3 = new Q("select * from eao_gbt where Father = ?", gbt3.getSType())
												.fetch();
										if (dt3.getRowCount() > 0) {
											reportTypeVo3.put("children", reportTypeVo3List);
											reportTypeVo3.put("loading", false);
										}
									}
									reportTypeVo2List.add(reportTypeVo3);
								}
								reportTypeVo2.put("children", reportTypeVo2List);
								reportTypeVo2.put("loading", false);
							} else {
								reportTypeVo2.put("children", reportTypeVo4List);
								reportTypeVo2.put("loading", false);
							}
							reportTypeVoList.add(reportTypeVo2);
						}
						reportTypeVo.put("children", reportTypeVoList);
						reportTypeVo.put("loading", false);
					} else {
						reportTypeVo.put("children", reportTypeVo4List);
						reportTypeVo.put("loading", false);
					}
					list.add(reportTypeVo);
				}
			} else {
				System.out.println("1111");
			}
			this.Response.put("list", list);
		}
	}

	/**
	 * 查询所有管理名录菜单列表
	 */
	@Priv(loginType = LoginType.Member)
	public void findAllMenu2() {
		DataTable dt = new Q(" select * from eao_management where Father = ' ' or Father is null ORDER BY sort ")
				.fetch();
		List<Map<String, Object>> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoManagement management = new EaoManagement();
			management.setValue(dataRow);
			Map<String, Object> reportTypeVo = new HashMap<>();
			reportTypeVo.put("value", management.getMid());
			reportTypeVo.put("label", management.getMid() + " " + management.getType());
			list.add(reportTypeVo);
			List<Map<String, Object>> reportTypeVo2List = new ArrayList<>();
			DataTable dt2 = new Q(" select * from eao_management where Father = ? ORDER BY sort", management.getMid())
					.fetch();
			if (dt2.getRowCount() > 0) {
				for (DataRow dataRow2 : dt2) {
					EaoManagement management2 = new EaoManagement();
					management2.setValue(dataRow2);
					Map<String, Object> reportTypeVo2 = new HashMap<>();
					reportTypeVo2.put("value", management2.getMid());
					reportTypeVo2.put("label", management2.getMid() + " " + management2.getType());
					reportTypeVo2List.add(reportTypeVo2);
					List<Map<String, Object>> reportTypeVo3List = new ArrayList<>();

					if (!reportTypeVo3List.isEmpty()) {
						reportTypeVo2.put("children", reportTypeVo3List);
					}
				}
			}
			if (!reportTypeVo2List.isEmpty()) {
				reportTypeVo.put("children", reportTypeVo2List);
			}
			this.Response.put("data", list);
		}
	}

}
